#include <err.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>

#include "../getopt_helper.h"

static struct option const LONG_OPTIONS[] = {
	{ "zero", no_argument, NULL, 'z' }, GETOPT_END_DECL
};

#define NFORMAT 4
static const char usage_str[] =
    "Usage: %s [OPTION] NAME...\n"
    "Output each NAME with its last non-slash component and trailing slashes\n"
    "removed; if NAME contains no /'s, output '.' (meaning the current "
    "directory).\n"
    "\n"
    "  -z, --zero     end each output line with NUL, not newline\n"
    "      --help     display this help and exit\n"
    "      --version  output version information and exit\n"
    "\n"
    "Examples:\n"
    "  %s /usr/bin/          -> '/usr'\n"
    "  %s dir1/str dir2/str  -> 'dir1' followed by 'dir2'\n"
    "  %s stdio.h            -> '.'";

int
main(int argc, char **argv)
{
	int ch;

	enum flag zero = NO;

	while ((ch = getopt_long(argc, argv, "z", LONG_OPTIONS, NULL)) != -1) {
		switch (ch) {
		case 'z':
			zero = YES;
			break;

			GETOPT_END_CASE
		}
	}

	if (argc <= optind) errx(!0, "missing operand");

	for (; optind < argc; optind++)
		printf("%s%c", dirname(argv[optind]), '\n' * !zero);

	return 0;
}
